package io.renren.wh.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.wh.interfaces.dao.InterfacesDao;
import io.renren.wh.interfaces.entity.InterfacesEntity;
import io.renren.wh.merchant.dao.MerchantKeyDao;
import io.renren.wh.merchant.entity.MerchantKeyEntity;
import io.renren.wh.merchant.entity.RateEntity;
import io.renren.wh.merchant.service.MerchantKeyService;
import io.renren.wh.merchant.service.RateService;
import io.renren.wh.merchant.vo.MerchantChannelRateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("merchantKeyService")
public class MerchantKeyServiceImpl extends ServiceImpl<MerchantKeyDao, MerchantKeyEntity> implements MerchantKeyService {

    @Autowired
    private RateService rateService;

    @Autowired
    private InterfacesDao interfacesDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MerchantKeyEntity> page = this.page(
                new Query<MerchantKeyEntity>().getPage(params),
                new QueryWrapper<MerchantKeyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public MerchantKeyEntity queryByeAppId(String appId) {
        return super.getOne(new QueryWrapper<MerchantKeyEntity>().eq("appid",appId));
    }

    @Override
    public MerchantKeyEntity queryByMerchantId(Integer merchantId) {

        MerchantKeyEntity merchantKeyEntity = this.baseMapper.selectOne(new QueryWrapper<MerchantKeyEntity>().eq("merchantid", merchantId));

        return merchantKeyEntity;
    }

    @Override
    public List<MerchantChannelRateVo> getChannelRateInfo(Integer merchantId) {

        List<MerchantChannelRateVo> channelRateVo = new ArrayList<>();

        //查询出商户费率信息
        List<RateEntity> rateEntityList = rateService.queryByMerchantId(merchantId);

        //拿到接口标识查询接口名称
        List<Integer> interCodes = rateEntityList.stream().map(e -> {
            return e.getInterfacecode();
        }).collect(Collectors.toList());

        //查询出接口信息
        List<InterfacesEntity> interfacesEntities =  interfacesDao.selectByInterCodes(interCodes);

        for (int i = 0; i < rateEntityList.size(); i++) {
            MerchantChannelRateVo vo = new MerchantChannelRateVo();
            BeanUtils.copyProperties(rateEntityList.get(i),vo);
            vo.setInterfacename(interfacesEntities.get(i).getInterfacename());
            channelRateVo.add(vo);
        }

        return channelRateVo;
    }

    @Override
    public MerchantKeyEntity selectByUserName(String userName) {

        MerchantKeyEntity merchantKeyEntity = this.baseMapper.selectOne(new QueryWrapper<MerchantKeyEntity>().eq("merchantaccount", userName));

        return merchantKeyEntity;
    }
}